<template>
  <div class="search-section" :style="backgroundStyle">
    <h1 class="title">{{ searchData.title }}</h1>
    <p class="subtitle">{{ searchData.description }}</p>

    <div class="search-bar">
      <div class="select-wrapper">
        <select v-model="selectedType">
          <option value="all">全部</option>
          <option :value="item.id" v-for="(item,index) in selectedList" :key="index">{{ item.name }}</option>
        </select>
        <i class="iconfont icon-xiangxia"></i>
      </div>
      <div class="input-wrapper">
        <input
            type="text"
            v-model="searchText"
            placeholder="搜索你感兴趣的内容..."
            @keyup.enter="handleSearch"
        >
      </div>
      <button class="search-button" @click="handleSearch">
        <svg class="search-icon" viewBox="0 0 24 24" width="20" height="20">
          <path
              d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"
              fill="currentColor"/>
        </svg>
      </button>
    </div>

    <div class="search-tags">
      搜索热门：
      <span
          v-for="tag in searchData.hotTags"
          :key="tag"
          class="tag"
          @click="handleTagClick(tag)"
      >
          {{ tag }}
        </span>
    </div>
  </div>
</template>

<script>
import {inquireCategory} from '@/api/portalAPI'
import constantsData from "@/constants";

export default {
  name: 'SearchBar',
  props: {
    isInSearchPage: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectedType: 'all',
      selectedList: [],
      searchText: '',
      searchData: {
        title: constantsData().searchTitle,
        description: constantsData().searchDescription,
        hotTags: constantsData().searchHot,
        background:constantsData().searchBackground
      },
      windowWidth: window.innerWidth
    }
  },
  computed: {
    backgroundStyle() {
      // 获取背景图片URL
      const imageUrl = this.getResponsiveImageUrl(this.searchData.background);
      
      return {
        backgroundImage: `linear-gradient(135deg, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.6) 100%), url('${imageUrl}')`,
        // 不在这里设置backgroundSize和backgroundPosition，让CSS样式接管
      }
    }
  },
  mounted() {
    this.init();
    // 添加窗口大小变化监听
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    // 移除事件监听器
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    handleResize() {
      this.windowWidth = window.innerWidth;
    },
    getResponsiveImageUrl(originalUrl) {
      if (!originalUrl) {
        // 如果没有提供URL，使用默认图片
        return require('@/assets/images/banner.jpg');
      }
      
      // 对于所有图片，我们不再根据屏幕宽度调整尺寸
      // 而是直接返回原始URL，让CSS的background-size: cover来处理缩放
      return originalUrl;
    },
    async getCategory() {
      try {
        const {code, data, msg} = await inquireCategory()
        if (code == 0) {
          this.selectedList = data;
        } else {
          console.error(msg)
        }
      } catch (error) {
        console.error(error)
      }
    },
    init() {
      this.getCategory();
    },
    handleSearch() {
      if (this.isInSearchPage) {
        this.$emit('search', {
          q: this.searchText,
          type: this.selectedType
        })
      } else {
        this.$router.push({
          path: '/search',
          query: {
            q: this.searchText,
            type: this.selectedType
          }
        })
      }
    },
    handleTagClick(tag) {
      this.searchText = tag
      this.handleSearch()
    }
  }
}
</script>

<style scoped>
.search-section {
  padding: 40px 0 30px;
  text-align: center;
  position: relative;
  width: 100vw;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
  margin-top: -70px;
  overflow: hidden;
  /* 添加平滑过渡效果 */
  transition: background-image 0.3s ease-in-out;
  /* 优化图片渲染 */
  image-rendering: auto; /* 改为auto以获得更好的放大效果 */
  will-change: background-image;
  
  /* 确保容器有足够的高度以显示完整的内容 */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  
  /* 确保背景图片完全覆盖容器 */
  background-size: 100% 100% !important; /* 强制图片拉伸以完全覆盖容器 */
  background-position: center center !important;
}

.search-section > * {
  position: relative;
  z-index: 2;
}

.title {
  font-size: 28px;
  color: #ffffff;
  margin-bottom: 12px;
  padding: 0 20px;
  margin-top: 70px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.subtitle {
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 24px;
  padding: 0 20px;
  font-size: 16px;
}

.search-bar {
  max-width: 800px;
  width: calc(100% - 40px);
  margin: 0 auto;
  padding: 0;
  display: flex;
  height: 50px;
  background: var(--card-bg);
  border-radius: 25px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.select-wrapper {
  position: relative;
  min-width: 80px;
  border-right: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  height: 100%;
}

.select-wrapper select {
  width: 100%;
  height: 100%;
  padding: 0 30px 0 12px;
  border: none;
  background: transparent;
  appearance: none;
  -webkit-appearance: none;
  color: var(--text-color);
  cursor: pointer;
  outline: none;
  font-size: 14px;
  text-align: left;
  transition: all 0.3s;
}

/* 自定义下拉箭头 */
.select-wrapper .icon-xiangxia {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--secondary-color);
  font-size: 12px;
  pointer-events: none;
  transition: all 0.3s;
}

/* 下拉框打开时箭头旋转 */
.select-wrapper select:focus + .icon-xiangxia {
  transform: translateY(-50%) rotate(180deg);
}

/* 悬停效果 */
.select-wrapper:hover select {
  background-color: rgba(0, 0, 0, 0.02);
}

/* 选中状态 */
.select-wrapper select:focus {
  background-color: rgba(0, 0, 0, 0.03);
}

/* 下拉选项样式 */
.select-wrapper select option {
  color: var(--text-color);
  padding: 8px 12px;
  min-height: 32px;
  line-height: 32px;
  font-size: 14px;
  background-color: var(--card-bg);
}

.select-wrapper select option:hover {
  background-color: rgba(var(--primary-color-rgb), 0.1);
}

/* 移动端适配优化 */
@media (max-width: 768px) {
  .select-wrapper {
    min-width: 70px;
  }

  .select-wrapper select {
    padding: 0 25px 0 10px;
    font-size: 13px;
  }

  .select-wrapper .icon-xiangxia {
    right: 6px;
    font-size: 11px;
  }
}

.input-wrapper {
  flex: 1;
  display: flex;
  height: 100%;
}

input {
  width: 100%;
  height: 100%;
  padding: 0 20px;
  border: none;
  background: transparent;
  color: var(--text-color);
  font-size: 15px;
  outline: none;
}

input::placeholder {
  color: var(--secondary-color);
}

.search-button {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 50px;
  height: 100%;
  background: var(--primary-color);
  border: none;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
  padding: 0 16px;
  margin: 0;
  flex-shrink: 0;
}

.search-icon {
  transition: transform 0.3s;
}

.search-button:hover .search-icon {
  transform: scale(1.1);
}

/* 修改移动端适配 */
@media (max-width: 768px) {
  .search-section {
    padding: 20px 10px;
  }

  .search-bar {
    width: calc(100% - 20px);
    height: 40px;
    border-radius: 20px;
  }

  .select-wrapper {
    min-width: 70px;
  }

  .select-wrapper select {
    padding: 0 20px 0 8px;
    font-size: 13px;
  }

  input {
    padding: 0 12px;
    font-size: 13px;
  }

  .search-button {
    min-width: 44px;
    padding: 0 12px;
  }

  .search-icon {
    width: 18px;
    height: 18px;
  }

  .search-tags {
    margin-top: 12px;
    padding: 0;
    font-size: 13px;
  }
}

/* 中等屏幕适配 */
@media (max-width: 480px) {
  .search-bar {
    height: 40px;
    border-radius: 20px;
  }

  .select-wrapper {
    min-width: 60px;
  }

  .select-wrapper select {
    padding: 0 18px 0 8px;
    font-size: 13px;
  }

  .button-text {
    display: none;
  }

  .search-button {
    min-width: 40px;
    padding: 0 12px;
  }
}

/* 小屏幕适配 */
@media (max-width: 375px) {
  .search-section {
    padding: 15px 8px;
  }

  .search-bar {
    height: 36px;
    border-radius: 18px;
  }

  .select-wrapper {
    min-width: 50px;
  }

  .select-wrapper select {
    padding: 0 16px 0 4px;
    font-size: 12px;
  }

  .search-button {
    min-width: 36px;
    padding: 0 10px;
  }

  .search-icon {
    width: 16px;
    height: 16px;
  }

  .search-tags {
    margin-top: 12px;
    padding: 0;
    font-size: 13px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 320px) {
  .search-bar {
    height: 34px;
    border-radius: 17px;
  }

  .select-wrapper {
    min-width: 45px;
  }

  .select-wrapper select {
    padding: 0 14px 0 2px;
    font-size: 12px;
  }

  .search-button {
    min-width: 32px;
    padding: 0 8px;
  }

  .search-icon {
    width: 14px;
    height: 14px;
  }

  .search-tags {
    margin-top: 12px;
    padding: 0;
    font-size: 13px;
  }
}

/* 添加横屏小屏幕适配 */
@media (max-height: 480px) and (orientation: landscape) {
  .search-bar {
    height: 36px;
  }

  .search-button {
    min-width: 36px;
    padding: 0 10px;
  }

  .search-button span {
    display: none;
  }
}

/* 修复移动端点击效果 */
.search-button {
  -webkit-tap-highlight-color: transparent;
  -webkit-appearance: none;
}

.search-button:active {
  opacity: 0.8;
}

.search-tags {
  margin-top: 15px;
  color: rgba(255, 255, 255, 0.8);
  padding: 0 20px;
}

.tag {
  display: inline-block;
  padding: 4px 12px;
  margin: 0 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
}

.tag:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .tag {
    padding: 3px 10px;
    margin: 0 4px;
    font-size: 13px;
  }
}

@media (max-width: 375px) {
  .search-tags {
    margin-top: 12px;
    font-size: 12px;
  }

  .tag {
    padding: 2px 8px;
    margin: 0 3px;
    font-size: 12px;
  }
}

/* 添加搜索页面特定样式 */
.search-bar.in-search-page {
  max-width: 100%;
  margin: 0;
  background: transparent;
}

.search-bar.in-search-page .search-section {
  padding: 0;
  margin: 0;
  background: none;
}

.search-bar.in-search-page .title,
.search-bar.in-search-page .subtitle,
.search-bar.in-search-page .search-tags {
  display: none;
}

/* 针对不同设备的背景图片高度调整 */
@media (max-width: 480px) {
  .search-section {
    padding: 30px 0 20px;
    margin-top: -50px;
    min-height: 200px; /* 确保小屏幕上有足够的高度 */
  }
  
  .title {
    margin-top: 40px;
  }
}

@media (min-width: 481px) and (max-width: 768px) {
  .search-section {
    padding: 35px 0 25px;
    margin-top: -60px;
    min-height: 250px;
  }
  
  .title {
    margin-top: 50px;
  }
}

@media (min-width: 769px) and (max-width: 1200px) {
  .search-section {
    min-height: 300px;
  }
}

@media (min-width: 1201px) {
  .search-section {
    min-height: 350px;
  }
}
</style>
